package com.yan.community.service.impl;

import static com.yan.club.utils.SnowflakeIdWorker.generateId;
import static com.yan.community.constant.Constant.NOT_DELETE;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yan.club.result.Result;
import com.yan.club.result.ResultCode;
import com.yan.community.dto.CommunityCategoryDto;
import com.yan.community.mapper.ClubCommunityCategoryMapper;
import com.yan.community.model.ClubCommunityCategory;
import com.yan.community.model.ClubCommunityCategoryExample;
import com.yan.community.service.CommunityCategoryService;

/**
 * @author 彦
 * @since 2023/12/12 14:54
 */
@Service
public class CommunityCategoryServiceImpl implements CommunityCategoryService {

    @Resource
    private ClubCommunityCategoryMapper clubCategoryMapper;

    /**
     * 得到全部社团分类
     *
     * @param clubCategoryDto
     * @return
     */
    @Override
    public Result<PageInfo<ClubCommunityCategory>> getAllCategory(CommunityCategoryDto clubCategoryDto) {

        PageHelper.startPage(clubCategoryDto.getPageNum(), clubCategoryDto.getPageSize(), true);

        ClubCommunityCategoryExample example = new ClubCommunityCategoryExample();
        ClubCommunityCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andIsDeleteEqualTo(NOT_DELETE);

        if (!StringUtils.isEmpty(clubCategoryDto.getName())) {
            criteria.andNameLike("%" + clubCategoryDto.getName() + "%");
        }

        List<ClubCommunityCategory> clubCategories = clubCategoryMapper.selectByExample(example);

        return Result.success(new PageInfo<>(clubCategories));
    }

    /**
     * 逻辑删除
     *
     * @param id
     * @return
     */
    @Override
    public Result<String> delCategory(Long id) {

        if (StringUtils.isEmpty(id)) {
            return Result.error(ResultCode.INTERNAL_SERVER_ERROR.code, "id为空,无法删除");
        }

        clubCategoryMapper.delCategory(id);
        return Result.success("删除成功");
    }

    /**
     * 新增 / 修改
     *
     * @param clubCategory
     * @return
     */
    @Override
    public Result<String> saveCategory(ClubCommunityCategory clubCategory) {

        if (ObjectUtils.isEmpty(clubCategory)) {
            return Result.error(ResultCode.INTERNAL_SERVER_ERROR.code, "参数为空,无法操作");
        }

        clubCategory.setUpdateTime(new Date());
        clubCategory.setIsDelete(NOT_DELETE);
        if (StringUtils.isEmpty(clubCategory.getId())) {
            clubCategory.setId(generateId());
            clubCategory.setCreateTime(new Date());
        } else {
            ClubCommunityCategory dbClubCommunityCategory = clubCategoryMapper.selectByPrimaryKey(clubCategory.getId());
            clubCategory.setCreateTime(dbClubCommunityCategory.getCreateTime());

            clubCategoryMapper.updateByPrimaryKey(clubCategory);

        }
        return Result.success();
    }

    /**
     * 根据ID查询种类
     *
     * @param id
     * @return
     */
    @Override
    public Result<ClubCommunityCategory> getCateGoryById(Long id) {
        if (StringUtils.isEmpty(id)) {
            return Result.error(ResultCode.INTERNAL_SERVER_ERROR.code, "参数为空,无法操作");
        }

        ClubCommunityCategory clubCategory = clubCategoryMapper.selectByPrimaryKey(id);
        return Result.success(clubCategory);
    }
}
